package org.celery.modules.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.celery.modules.goods.entity.GoodsSpecification;
import org.celery.modules.goods.service.IGoodsSpecificationService;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.goods.entity.GoodsSpecificationData;
import org.celery.modules.goods.vo.GoodsSpecificationDataVO;
import org.celery.modules.goods.wrapper.GoodsSpecificationDataWrapper;
import org.celery.modules.goods.service.IGoodsSpecificationDataService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品规格数据表 控制器
 *
 * @author Celery
 * @since 2021-10-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-goods/goods-specification-data")
@Api(value = "商品规格数据表", tags = "商品规格数据表接口")
public class GoodsSpecificationDataController extends BladeController {

	private final IGoodsSpecificationDataService goodsSpecificationDataService;
	private final IGoodsSpecificationService goodsSpecificationService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入goodsSpecificationData")
	public R<GoodsSpecificationDataVO> detail(GoodsSpecificationData goodsSpecificationData) {
		GoodsSpecificationData detail = goodsSpecificationDataService.getOne(Condition.getQueryWrapper(goodsSpecificationData));
		return R.data(GoodsSpecificationDataWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商品规格数据表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsSpecificationData")
	public R<IPage<GoodsSpecificationDataVO>> list(GoodsSpecificationData goodsSpecificationData, Query query) {
		QueryWrapper<GoodsSpecificationData> queryWrapper = Condition.getQueryWrapper(goodsSpecificationData);
		queryWrapper.orderByAsc("sort");
		IPage<GoodsSpecificationData> pages = goodsSpecificationDataService.page(Condition.getPage(query), queryWrapper);
		return R.data(GoodsSpecificationDataWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商品规格数据表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入goodsSpecificationData")
	public R<IPage<GoodsSpecificationDataVO>> page(GoodsSpecificationDataVO goodsSpecificationData, Query query) {
		IPage<GoodsSpecificationDataVO> pages = goodsSpecificationDataService.selectGoodsSpecificationDataPage(Condition.getPage(query), goodsSpecificationData);
		return R.data(pages);
	}

	/**
	 * 新增 商品规格数据表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入goodsSpecificationData")
	public R save(@Valid @RequestBody GoodsSpecificationData goodsSpecificationData) {
		return R.status(goodsSpecificationDataService.save(goodsSpecificationData));
	}

	/**
	 * 修改 商品规格数据表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入goodsSpecificationData")
	public R update(@Valid @RequestBody GoodsSpecificationData goodsSpecificationData) {
		return R.status(goodsSpecificationDataService.updateById(goodsSpecificationData));
	}

	/**
	 * 新增或修改 商品规格数据表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入goodsSpecificationData")
	public R submit(@Valid @RequestBody GoodsSpecificationData goodsSpecificationData, BladeUser bladeUser) {
		if (Func.isNotEmpty(goodsSpecificationData.getId())) {
			GoodsSpecification goodsSpecification = goodsSpecificationService.getById(goodsSpecificationData.getGoodsSpecificationId());
			if (Func.isNotEmpty(goodsSpecification) && !goodsSpecification.getTenantId().equals(bladeUser.getTenantId())) {
				throw new ServiceException("没有操作权限");
			}
		}
		return R.status(goodsSpecificationDataService.saveOrUpdate(goodsSpecificationData));
	}

	
	/**
	 * 删除 商品规格数据表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids, BladeUser bladeUser) {
		List<GoodsSpecificationData> goodsSpecificationDataList = goodsSpecificationDataService.list(
				Wrappers.<GoodsSpecificationData>lambdaQuery()
						.in(GoodsSpecificationData::getId, ids)
		);
		List<Long> goodSpecificationIdList = goodsSpecificationDataList.stream()
				.map(GoodsSpecificationData::getGoodsSpecificationId).collect(Collectors.toList());
		List<GoodsSpecification> goodsSpecificationList = goodsSpecificationService.list(
				Wrappers.<GoodsSpecification>lambdaQuery().in(GoodsSpecification::getId, goodSpecificationIdList)
		);
		for (GoodsSpecification goodsSpecification : goodsSpecificationList) {
			if (!goodsSpecification.getTenantId().equals(bladeUser.getTenantId())) {
				throw new ServiceException("没有操作权限");
			}
		}
		return R.status(goodsSpecificationDataService.deleteLogic(Func.toLongList(ids)));
	}

	
}
